<?php

namespace App\Http\Controllers\BG;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

use  App\Models\User;
use  App\Models\UserGroup;

use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;


class UserGroupController extends Controller
{
    public function list(Request $request)
    {
        $input = Input::only('pid');
        $validator = Validator::make($input, [        
            'pid'     =>  'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $user_group = UserGroup::where('id','>',0);
        if(isset($input['pid'])){
            $user_group->where('pid',$input['pid']); 
        }
        $data = $user_group->get();
        return $this->returnOk($data);
    }


    public function create(Request $request)
    {
        $input = Input::only('name', 'pid','description','model');
        $validator = Validator::make($input, [        
            'name'      => 'required',
            'pid'         => 'nullable',
            'description' => 'nullable',
            'model' => 'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $data = $input;
        $where['name'] = $input['name'];
        $exist = UserGroup::where($where)->first();
        if($exist){
            return $this->returnError([],409,'分组已存在');
        }else{
            $data['pid'] = $data['pid']??0;
            UserGroup::create($data);
        }
        return $this->returnOk();
    }

    public function update(Request $request)
    {
        $input = Input::only('id','name', 'pid','description','model');
        $validator = Validator::make($input, [        
            'id'      => 'required|integer',
            'name'      => 'required',
            'pid'         => 'nullable',
            'description' => 'nullable',
            'model' => 'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $group = UserGroup::where('id',$input['id'])->first();
        if($group){
            $exist = UserGroup::where('name',$input['name'])->where('id','!=',$input['id'])->first();
            if($exist){
                return $this->returnError([],409,'分组已存在');
            }
            $group->name = $input['name'];
            $group->pid = isset($input['pid'])?$input['pid']:$group->pid;
            $group->description = isset($input['description'])?$input['description']:$group->description;
            $group->model = isset($input['model'])?$input['model']:$group->model;
            $group->save();
        }else{
            return $this->returnError([],409,'分组不存在');
        }
        return $this->returnOk();
    }

    public function delete()
    {
        $input = Input::only('id');
        $validator = Validator::make($input, [   
            'id'      => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
       
        $where['id'] = $input['id'];
        $exist = UserGroup::where($where)->first();
        if($exist){
            $exist->delete();
        }else{
            return $this->returnError([],409,'分组不存在');
        }
        return $this->returnOk();
    }
}